/*
* $Id: Application.cpp 75 2010-03-26 12:28:48Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Application.h"
#include "Database/DatabaseManager.h"
#include "Database/Tables/InspectTable.h"
#include "Database/Tables/EvacuationTable.h"
#include "Database/Tables/FamiliesTable.h"
#include "Database/Tables/FieldValuesTable.h"
#include "Database/Tables/HousingsTable.h"
#include "Database/Tables/JournalTable.h"
#include "Database/Tables/PointsTable.h"
#include "Database/Tables/QueenBeeTable.h"
#include "Database/Tables/UserFieldsTable.h"
#include "Language/LanguageManager.h"
#include "Visual/VisualManager.h"
#include "Settings/SettingsManager.h"
#include "Components/Components.h"
#include "Event/EventManager.h"

static IApplication* g_App = NULL;

IApplication* IApplication::GetApp(){
	if(!g_App)
		g_App = new CApplication;
	return g_App;
}

CApplication::CApplication(){}
CApplication::~CApplication(){
	m_pVisualManager->Free();
	m_pSettingsManager->Free();
	m_pDatabaseManager->Free();
	for(std::vector<IComponent*>::iterator it = m_aComponents.begin(); it != m_aComponents.end(); it++)
		(*it)->Free();
	m_pLanguageManager->Free();
	m_pEventManager->Free();
}
BOOL CApplication::InitInstance(){
//Create main components
	m_pEventManager = new CEventManager;
	m_pLanguageManager = new CLanguageManager;
	m_pDatabaseManager = new CDatabaseManager;
//Create database if it doesn't exist
	IDatabase* pDatabase = m_pDatabaseManager->OpenDatabase(JOURNAL_DATABASE_NAME);
	CFieldValuesTable table3;
	table3.CreateTable(pDatabase);
	CPointsTable table8;
	table8.CreateTable(pDatabase);
	CQueenBeeTable table6;
	table6.CreateTable(pDatabase);
	CFamiliesTable table2;
	table2.CreateTable(pDatabase);
	CEvacuationTable table1;
	table1.CreateTable(pDatabase);
	CHousingsTable table4;
	table4.CreateTable(pDatabase);
	CInspectTable table;
	table.CreateTable(pDatabase);
	CJournalTable table5;
	table5.CreateTable(pDatabase);
	CUserFieldsTable table7;
	table7.CreateTable(pDatabase);
	m_pDatabaseManager->CloseDatabase(pDatabase);
//Create main components
	m_aComponents.push_back(new CSettingsComponent);
	m_aComponents.push_back(new CJournalComponent);
	m_pSettingsManager = new CSettingsManager;
	m_pVisualManager = new CVisualManager;
//Init common controls
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);
	CWinApp::InitInstance();
//Initialize language manager
	m_pLanguageManager->Init();
//Create main window
	m_pMainWnd = dynamic_cast<CWnd*>(m_pVisualManager->GetMainWindow());
	WNDCLASS wndcls;
	wndcls.style = CS_VREDRAW | CS_HREDRAW;
	wndcls.lpfnWndProc = ::DefWindowProc;
	wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
	wndcls.hInstance = GetModuleHandle(NULL);
	wndcls.hIcon = ::LoadIcon(NULL, MAKEINTRESOURCE(IDR_MAINFRAME));
	wndcls.hCursor = ::LoadCursor(NULL, IDC_ARROW);
	wndcls.hbrBackground = NULL;
	wndcls.lpszMenuName = NULL;
	wndcls.lpszClassName = MAINCLASS_WINDOW;
	AfxRegisterClass(&wndcls);
	((CFrameWnd*)m_pMainWnd)->Create(MAINCLASS_WINDOW,  m_pLanguageManager->GetString(MAIN_FRAME_CAPTION_ID).c_str(), WS_VISIBLE, CRect(CPoint(CW_USEDEFAULT, CW_USEDEFAULT), CSize(520, 545)));
	return TRUE;
}

IDatabaseManager* CApplication::GetDatabaseManager(){
	return m_pDatabaseManager;
}
ILanguageManager* CApplication::GetLanguageManager(){
	return m_pLanguageManager;
}
ISettingsManager* CApplication::GetSettingsManager(){
	return m_pSettingsManager;
}
IVisualManager* CApplication::GetVisualManager(){
	return m_pVisualManager;
}
IEventManager* CApplication::GetEventManager(){
	return m_pEventManager;
}
std::vector<IComponent*> CApplication::GetComponents(){
	return m_aComponents;
}
void CApplication::Free(){
	delete this;
}
